package jsyf.oa.dao.wreport;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.oa.entity.wreport.ModuleEntity;
import jsyf.oa.idao.BaseDao;
import jsyf.oa.ientity.IBaseEntity;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by Lenovo on 2017/11/24.
 */
@Repository
public class ModuleDao extends BaseDao {
    public ModuleDao() {
        super.iBaseEntity = new ModuleEntity();
    }

    @Override
    public IBaseEntity convertJson2Entity(JSONObject json) {
        return JSON.toJavaObject(json, ModuleEntity.class);
    }

    public List<JSONObject> getModuleENamesByUserId(String userId)
    {
        String sql="SELECT\n" +
                "\tT_WRT_MODULE.ID AS id,T_WRT_MODULE.`NAME` AS name,T_WRT_MODULE.E_NAME AS eName\n" +
                "FROM\n" +
                "\tT_WRT_MODULE\n" +
                "WHERE\n" +
                "\tID IN (\n" +
                "\t\tSELECT\n" +
                "\t\t\tMODULE_ID\n" +
                "\t\tFROM\n" +
                "\t\t\tT_WRT_TEMPLATE_2_MODULE\n" +
                "\t\tWHERE\n" +
                "\t\t\tTEMPLATE_ID = (\n" +
                "\t\t\t\tSELECT\n" +
                "\t\t\t\t\tID\n" +
                "\t\t\t\tFROM\n" +
                "\t\t\t\t\tT_WRT_TEMPLATE\n" +
                "\t\t\t\tWHERE\n" +
                "\t\t\t\t\tID = (\n" +
                "\t\t\t\t\t\tSELECT\n" +
                "\t\t\t\t\t\t\tTEMPLATE_ID\n" +
                "\t\t\t\t\t\tFROM\n" +
                "\t\t\t\t\t\t\tT_SYS_ROLE_NAME\n" +
                "\t\t\t\t\t\tWHERE\n" +
                "\t\t\t\t\t\t\tID = (\n" +
                "\t\t\t\t\t\t\t\tSELECT\n" +
                "\t\t\t\t\t\t\t\t\tROLE_NAME_ID\n" +
                "\t\t\t\t\t\t\t\tFROM\n" +
                "\t\t\t\t\t\t\t\t\tT_SYS_ROLE\n" +
                "\t\t\t\t\t\t\t\tWHERE\n" +
                "\t\t\t\t\t\t\t\t\tID = (\n" +
                "\t\t\t\t\t\t\t\t\t\tSELECT\n" +
                "\t\t\t\t\t\t\t\t\t\t\tROLE_ID\n" +
                "\t\t\t\t\t\t\t\t\t\tFROM\n" +
                "\t\t\t\t\t\t\t\t\t\t\tT_SYS_USER\n" +
                "\t\t\t\t\t\t\t\t\t\tWHERE\n" +
                "\t\t\t\t\t\t\t\t\t\t\tID = ?\n" +
                "\t\t\t\t\t\t\t\t\t)\n" +
                "\t\t\t\t\t\t\t)\n" +
                "\t\t\t\t\t)\n" +
                "\t\t\t)\n" +
                "\t)OR ID IN (SELECT MODULE_ID FROM t_sys_user_2_wreport WHERE USER_ID='"+ userId  + "')\n ";

         List<JSONObject> list= super.queryForJsonList(sql,new Object[]{userId});
         if(list.size()>0)
         {
             return  list;
         }
         else {
             String sqltemplate ="SELECT\n" +
                     "\tT_WRT_MODULE.ID AS id,\n" +
                     "\tT_WRT_MODULE.`NAME` AS NAME,\n" +
                     "\tT_WRT_MODULE.E_NAME AS eName\n" +
                     "FROM\n" +
                     "\tT_WRT_MODULE\n" +
                     "WHERE\n" +
                     "\tID IN (\n" +
                     "\t\tSELECT\n" +
                     "\t\t\tMODULE_ID\n" +
                     "\t\tFROM\n" +
                     "\t\t\tT_WRT_TEMPLATE_2_MODULE\n" +
                     "\t\tWHERE\n" +
                     "\t\t\tT_WRT_TEMPLATE_2_MODULE.TEMPLATE_ID = '2017112614024602802C0A92AC550741'\n" +
                     "\t)\n" +
                     "\n" +
                     "\n";
             return  super.queryForJsonList(sqltemplate.toString());
         }
    }
}
